

var vm = new Vue({
    el:'#setting',
    data:{
        showList: true,
        title: null,
        project: {},
        checkProject:{pId:0},
        projectList:[],
        itemList:[],
        deletePro:{},
        overlays:[]
    },
    methods: {
        query: function () {
            this.reload({parentId:0,type:0});
        },
        add: function(){
            vm.project.parentId=0;
            vm.project.type=0;
            vm.project.orderNum=0;
            vm.saveOrUpdate();
        },
        addArer:function () {
            if(vm.checkProject.pId==0||vm.checkProject.pId==null){
                alert("请选择项目");
            }else if(vm.overlays.length==0&&vm.deletePro.pId==null){
                vm.project.type=1;
                vm.project.orderNum=0;
                vm.project.parentId=vm.checkProject.pId;
                vm.saveOrUpdate();
            }

            if(vm.deletePro.pid>0){
                vm.project=vm.deletePro;
                vm.project.pId=vm.deletePro.pid;
                vm.project.remark=JSON.stringify(vm.overlays);
                if(vm.overlays==""){
                    vm.project.remark=null;
                }
                vm.overlays=[];
                vm.saveOrUpdate();
            }

        },
        update: function (event) {
            var pId = getSelectedRow();
            if(pId == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(pId);
        },
        saveOrUpdate: function (event) {
            var url = vm.project.pId == null ? "xinpai/project/save" : "xinpai/project/update";
            var data=JSON.parse(JSON.stringify(vm.project));
            vm.project={};
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(data),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            if(data.parentId>0&&data.pid==null){
                                vm.selectInfo();
                            }else if(data.parentId>0&&data.pid>0){

                            }else{
                                vm.reload({parentId:0,type:0});
                            }
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            if(isBlank(this.deletePro.pid)){
                alert("没有选中任何记录")
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "xinpai/project/delete",
                    contentType: "application/json",
                    data: JSON.stringify(vm.deletePro),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                if(vm.deletePro.type==1){
                                    vm.selectInfo()
                                }else{
                                    vm.query();
                                }
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function(pId){
            $.get(baseURL + "xinpai/project/info/"+pId, function(r){
                vm.project = r.project;
            });
        },
        reload: function (data) {
            $.ajax({
                type: "GET",
                url:  baseURL + 'xinpai/project/list',
                contentType: "application/json",
                data: data,
                success: function(r){
                    if(r.code === 0){
                        r.list.forEach(function (obj) {
                            obj.check=false;
                        })
                        if(data.parentId>0){
                            vm.itemList=r.list;
                        }else{
                            vm.projectList=r.list;
                        }

                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        check:function (index,tick) {
            var list=[];
            map.clearMap();
            vm.overlays=[];
            if(index==-1){
                vm.deletePro.pid=null;
            }
            if(tick==1){
                list=vm.itemList;
                for (var i = 0; i < vm.projectList.length; i++) {
                    var obj =vm.projectList[i];
                    obj.check=false;
                }
            }else{
                list=vm.projectList;
                for (var i = 0; i < vm.itemList.length; i++) {
                    var obj =vm.itemList[i];
                        obj.check=false;
                }
            }
            list.forEach(function (obj) {
                obj.check=false;
            })
            for (var i = 0; i < list.length; i++) {
                var obj =list[i];
                if(i==index){
                    obj.check=true;
                    vm.deletePro=obj;
                    if(obj.remark!=""&&obj.remark!=null){
                        var path=JSON.parse(obj.remark);
                        vm.overlays=JSON.parse(obj.remark);
                        for (var j = 0; j < path.length; j++) {
                            var o = path[j];
                            var newLine = new AMap.Polyline({
                                path:o,
                                strokeColor:'red'
                            })
                            newLine.setMap(map);
                        }

                        map.setFitView()
                    }
                }else{
                    obj.check=false;
                }
            }
        },
        selectInfo:function () {
            vm.deletePro={};
            vm.overlays=[];
            this.reload({parentId:this.checkProject.pId,type:1});
        },
        draw:function () {
            mouseTool.polyline({
                strokeColor:'red'
                //同Polyline的Option设置
            });
        },
        clear:function () {
            vm.overlays=[];
            map.clearMap();
        }
    },
    created:function () {
        this.query();
    }
});
var map = new AMap.Map("mapcontainer", {
    resizeEnable: true,
    center: [119.591115, 32.429234],
    zoom: 14
});
var mouseTool = new AMap.MouseTool(map);
mouseTool.on('draw',function(e){
    let path=[];
    for (var i = 0; i < e.obj.getPath().length; i++) {
        var obj = e.obj.getPath()[i];
        path.push([obj.lng,obj.lat]);
    }
    vm.overlays.push(path)
})
